﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Management;
using System.Configuration;

namespace IPR_TrainExamSys_TY
{
    public partial class FrmRegister : Form
    {
        string sRegReSult = "";
        bool bIsReg = false;
        string sMachineCode="";
        string sRegCode = "";
        public FrmRegister(string RegSult, bool IsReg,string MachineCode,string RegCode)
        {
            InitializeComponent();
            sRegReSult = RegSult;
            bIsReg = IsReg;
            sMachineCode = MachineCode;
            sRegCode = RegCode;
        }

        private void btnSaveToTxt_Click(object sender, EventArgs e)
        {

            string strFilePath = Application.StartupPath + "\\MachinaCode.txt";
            StreamWriter sw = new StreamWriter(strFilePath, false, System.Text.Encoding.Default);
            sw.WriteLine(txtMechineCode.Text.Trim());
            sw.Close();
            MessageBox.Show("文件已保存,位置 " + strFilePath);
        }

        private void FrmRegister_Load(object sender, EventArgs e)
        {
            lbReg.Text = sRegReSult; 
            lbReg.ForeColor = Color.Red;
            txtRegCode.Text = sRegCode;
            txtMechineCode.Text = sMachineCode;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtRegCode.Text.Trim() == "")
            {
                MessageBox.Show("请输入开发商提供的注册码！", "提示");
                return;
            }
            string file = System.Windows.Forms.Application.ExecutablePath;
            

            Configuration config = ConfigurationManager.OpenExeConfiguration(file);

            bool exist = false; //记录该连接串是否已经存在  
            //如果要更改的连接串已经存在  
            if (config.ConnectionStrings.ConnectionStrings["ConStringEncrypt"] != null)
            {
                exist = true;
            }
            // 如果连接串已存在，首先删除它  
            if (exist)
            {
                config.ConnectionStrings.ConnectionStrings.Remove("ConStringEncrypt");
            }

            ConnectionStringSettings mySettings =
                new ConnectionStringSettings("ConStringEncrypt",txtRegCode.Text.Trim());
            // 将新的连接串添加到配置文件中.  
            config.ConnectionStrings.ConnectionStrings.Add(mySettings);
            // 保存对配置文件所作的更改  
            config.Save(ConfigurationSaveMode.Modified);
            // 强制重新载入配置文件的ConnectionStrings配置节  
            ConfigurationManager.RefreshSection("ConStringEncrypt");
             
            if (MessageBox.Show("注册成功，请重新登录！", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                this.Close();
            }
        } 
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
